package club.emergency.wechat.question.search.provider.impl;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.model.option.ChoiceOption;
import club.emergency.wechat.model.question.Question;
import club.emergency.wechat.persistence.option.ChoiceOptionMapper;
import club.emergency.wechat.persistence.question.QuestionMapper;
import club.emergency.wechat.question.search.QuestionSearch;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.question.search.provider.impl
 * Date: 2018/11/28
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@RestController
public class QuestionSearchImpl implements QuestionSearch {

    private QuestionMapper questionMapper;

    private ChoiceOptionMapper choiceOptionMapper;

    public QuestionSearchImpl(
            QuestionMapper questionMapper,
            ChoiceOptionMapper choiceOptionMapper
    ) {
        this.questionMapper = questionMapper;
        this.choiceOptionMapper = choiceOptionMapper;
    }

    @Override
    public String getById(@RequestParam("id") Long id) {
        Question question = questionMapper.selectById(id);
        QueryWrapper<ChoiceOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", question.getId());
        List<ChoiceOption> choiceOptionList = choiceOptionMapper.selectList(queryWrapper);
        question.setChoiceOptionList(choiceOptionList);
        return ResponseEntity.ok(StatusCode.DB_SEARCH_OK, "查询单个题目数据成功！", question);
    }

    @Override
    public String flipListByCategoryAlias(@RequestParam(value = "categoryAlias", required = false) String categoryAlias, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        //查询问题数据
        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
        questionQueryWrapper.eq(Objects.nonNull(categoryAlias), "category_alias", categoryAlias);
        questionQueryWrapper.orderByDesc("modify_time");
        Page<Question> page = new Page<>(pageNo, pageSize);
        IPage<Question> questionIPage = questionMapper.selectPage(page, questionQueryWrapper);
        return ResponseEntity.ok(StatusCode.DB_SEARCH_OK, "查询题目数据成功！", questionIPage);
    }

    @Override
    public String getChoiceOptionByParent(@RequestParam("parentId") Long parentId) {
        QueryWrapper<ChoiceOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        queryWrapper.orderByAsc("id");
        List<ChoiceOption> choiceOptionList = choiceOptionMapper.selectList(queryWrapper);
        return ResponseEntity.ok(StatusCode.DB_SEARCH_OK, "根据题目id查询选项数据成功！", choiceOptionList);
    }

    @Override
    public String getRandomQuestion(@RequestParam("size") Integer size) {
        List<Long> allIds = questionMapper.getALLIds();
        if (allIds.size() <= size) {
            List<Question> questions = questionMapper.selectBatchIds(allIds);
            return ResponseEntity.ok(StatusCode.DB_SEARCH_OK, "随机题目查询成功！", questions);
        }
        List<Long> resultList = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < size; i++) {
            int index = random.nextInt(allIds.size());
            resultList.add(allIds.get(index));
            allIds.remove(index);
        }
        List<Question> questions = questionMapper.selectBatchIds(resultList);
        return ResponseEntity.ok(StatusCode.DB_SEARCH_OK, "随机题目查询成功！", questions);
    }

}